import { UIType } from "./Enum";
import StaticInstance from "./StaticInstance";
import ControlPanel from "./ui/ControlPanel";
import LevelInfo from "./ui/LevelInfo";
import LevelSelect from "./ui/LevelSelect";
import LosePanel from "./ui/LosePanel";
import StartMenu from "./ui/StartMenu";
import UIBase from "./ui/UIBase";
import WinPanel from "./ui/WinPanel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UIManager extends cc.Component {

    // ui prefabs
    @property(cc.Prefab)
    startMenuPrefab: cc.Prefab = null
    @property(cc.Prefab)
    levelSelectPrefab: cc.Prefab = null
    @property(cc.Prefab)
    controlPrefab: cc.Prefab = null
    @property(cc.Prefab)
    levelInfoPrefab: cc.Prefab = null
    @property(cc.Prefab)
    winPanelPrefab: cc.Prefab = null
    @property(cc.Prefab)
    losePanelPrefab: cc.Prefab = null

    private _uiRoot: cc.Node = null
    private _uiMap: Map<UIType, UIBase> = new Map()

    onLoad() {
        console.log(`UIManager onLoad`)
        StaticInstance.setUIManager(this)
        this._uiRoot = this.node

        // 初始化界面
        this._initStartMenu()
        this._initLevelSelect()
        this._initControlPanel()
        this._initLevelInfo()
        this._initWinPanel()
        this._initLosePanel()
    }

    public startGame(level: number) {
        console.log(`startGame`)
        StaticInstance.gameManager.startGame(level)
        this.showUI([UIType.CONTROL_PANEL, UIType.LEVEL_INFO])
    }

    public toStartMenu() {
        this.showUI([UIType.START_MENU])
    }

    public toLevelSelect() {
        console.log(`toLevelSelect`)
        this.showUI([UIType.LEVEL_SELECT])
    }

    public showWinPanel() {
        this.showUI([UIType.WIN_PANEL])
    }

    public showWinByLastPanel() {
        this.showUI([UIType.WIN_LAST_PANEL])
    }

    public showLosePanel() {
        this.showUI([UIType.LOSE_PANEL])
    }

    /**
     * 下一关
     */
    public onClickNextLevel() {
        StaticInstance.gameManager.onClickNextLevel()
    }

    /**
     * 再来一次
     */
    public onClickPlayAgain() {
        StaticInstance.gameManager.onClickPlayAgain()
    }

    /**
     * 返回首页
     */
    public onClickToStartMenu() {
        const gameMgr = StaticInstance.gameManager
        gameMgr.foodsRoot.removeAllChildren()
        gameMgr.hideBowl()
        this.showUI([UIType.START_MENU])
    }

    /**
     * 设置关卡左上角的关卡数据
     * @param level 
     * @param nowItem 
     * @param allNum 
     */
    public setLevelInfo(level: number, nowItem: number, allNum: number) {
        const levelInfoUI = this._uiMap.get(UIType.LEVEL_INFO) as LevelInfo
        levelInfoUI.setLevelLabel(level - 1)
        levelInfoUI.setItemsLabel(nowItem, allNum)
    }

    /**
     * 显示指定UI界面，隐藏其他UI界面
     * @param showTypes 
     */
    public showUI(showTypes: UIType[]) {
        this._uiMap.forEach((ui, uiType) => {
            if (showTypes.includes(uiType)) {
                ui.show()
            } else {
                ui.hide()
            }
        })
    }

    /**
     * 食物旋转角度
     * @param angle 
     */
    public onRotateFood(angle: number) {
        // 传递angle给GameManager
        StaticInstance.gameManager.onRotateFood(angle)
    }

    /**
     * 食物左移动
     * @param dt 
     */
    public onClickLeftFood(dt: number) {
        StaticInstance.gameManager.onClickLeftFood(dt)
    }

    /**
     * 食物右移动
     * @param dt 
     */
    public onClickRightFood(dt: number) {
        StaticInstance.gameManager.onClickRightFood(dt)
    }

    /**
     * 食物落下
     */
    public onClickDownFood() {
        StaticInstance.gameManager.onClickDownFood()
    }

    /**
     * 创建一个UI节点，挂载到UIManager节点上
     * @param prefab 节点预制体
     * @param component 节点上的脚本组件，设定后会尝试调用其init()方法（如果存在的话）
     */
    private _createUINode<T extends cc.Component>(prefab: cc.Prefab, component?: ({ prototype: T } | string)): cc.Node {
        // 创建节点
        const node: cc.Node = cc.instantiate(prefab)
        node.setParent(this._uiRoot)
        node.setPosition(0, 0)

        // 获取脚本组件
        let comp = null
        if (typeof component === 'string') {
            comp = node.getComponent(component)
        } else if (component) {
            comp = node.getComponent(component)
        }

        if (comp && comp.init) {
            comp.init(this)
        }

        return node
    }

    /**
     * 初始化-开始界面
     */
    private _initStartMenu() {
        const startMenu = this._createUINode(this.startMenuPrefab, StartMenu)
        const comp = startMenu.getComponent(StartMenu)
        this._uiMap.set(UIType.START_MENU, comp)
    }

    /**
     * 初始化-关卡选择界面
     */
    private _initLevelSelect() {
        const levelSelect = this._createUINode(this.levelSelectPrefab, LevelSelect)
        const comp = levelSelect.getComponent(LevelSelect)
        this._uiMap.set(UIType.LEVEL_SELECT, comp)
    }

    /**
     * 初始化-控制轮盘
     */
    private _initControlPanel() {
        const controlPanel = this._createUINode(this.controlPrefab, ControlPanel)
        const comp = controlPanel.getComponent(ControlPanel)
        this._uiMap.set(UIType.CONTROL_PANEL, comp)
    }

    /**
     * 初始化-关卡信息
     */
    private _initLevelInfo() {
        const levelInfo = this._createUINode(this.levelInfoPrefab, LevelInfo)
        const comp = levelInfo.getComponent(LevelInfo)
        this._uiMap.set(UIType.LEVEL_INFO, comp)
    }

    /**
     * 初始化-胜利界面
     */
    private _initWinPanel() {
        const winPanel = this._createUINode(this.winPanelPrefab, WinPanel)
        const comp = winPanel.getComponent(WinPanel)
        this._uiMap.set(UIType.WIN_PANEL, comp)
    }

    /**
     * 初始化-失败界面
     */
    private _initLosePanel() {
        const losePanel = this._createUINode(this.losePanelPrefab, LosePanel)
        const comp = losePanel.getComponent(LosePanel)
        this._uiMap.set(UIType.LOSE_PANEL, comp)
    }
}
